package com.luoying.itew.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luoying.itew.R;
import com.luoying.itew.bean.AddressOnItemClickListener;

import java.util.List;


/**
 * Created by Administrator on 2018/5/25.
 * time 2018-5-10
 * author zm
 */
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {
    private Context mContext;
    private List<String> mList;
    private AddressOnItemClickListener onItemClickListener;
    private int selectIndex = -1;

    public AddressAdapter(Context context, List<String> poiList) {
        this.mContext = context;
        this.mList = poiList;
    }

    @Override
    public AddressAdapter.AddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_item, parent, false);
        return new AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AddressAdapter.AddressViewHolder holder, int position) {
        holder.addressTv.setText(mList.get(position));
        if(selectIndex==position){
            holder.checkIv.setVisibility(View.VISIBLE);
        } else {
            holder.checkIv.setVisibility(View.INVISIBLE);
        }
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    if(position!=selectIndex){
                        if(selectIndex>=0){
                            notifyItemChanged(selectIndex);
                        }
                        notifyItemChanged(position);
                    }
                    selectIndex = position;
                    onItemClickListener.onItemClick(mList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setOnItemClickListener(AddressOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class AddressViewHolder extends RecyclerView.ViewHolder {
        private View rootView;
        public TextView addressTv;
        public ImageView checkIv;

        public AddressViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            addressTv = (TextView) itemView.findViewById(R.id.tv_address_name);
            checkIv = (ImageView) itemView.findViewById(R.id.iv_address_yes);
        }
    }
}
